package com.baosight.xcyber.tm.re.domain;

import com.alibaba.fastjson.JSONObject;
import com.zeroc.IceGrid.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class UIServer extends UITreeNode {
    public static Logger logger = LoggerFactory.getLogger(UIServer.class);
    ServerDescriptor serverDescriptor;

    public UIServer(ServerInstanceDescriptor serverInstanceDescriptor,
                    Map<String, TemplateDescriptor> serverTemplates,
                    Map<String,TemplateDescriptor> serviceTemplates){
        this.type="SERVER";
        this.serverTemplates=serverTemplates;
        this.serviceTemplates=serviceTemplates;
        String templateName = serverInstanceDescriptor.template;
        TemplateDescriptor templateDescriptor = this.getServerTemplates().get(templateName);

        Map<String, String> paramMap = new HashMap<>();
        paramMap.putAll(templateDescriptor.parameterDefaults);
        paramMap.putAll(serverInstanceDescriptor.parameterValues);
        if(IceBoxDescriptor.class.isAssignableFrom(templateDescriptor.descriptor.getClass())){
            IceBoxDescriptor descriptor = (IceBoxDescriptor) templateDescriptor.descriptor.clone();
            replaceObject(descriptor, paramMap);
            this.serverDescriptor = descriptor;
            this.name=this.serverDescriptor.id;
            List<AdapterDescriptor> adapters = this.serverDescriptor.adapters;
            List<UIAdapter> collect = adapters.stream().map(
                    UIAdapter::new).collect(Collectors.toList());
            this.getChildren().addAll(collect);
            if(descriptor.services!=null){
                List<UIService> collect2 = descriptor.services.stream().map(
                        item -> new UIService("", item,serverTemplates,serviceTemplates,paramMap)).collect(Collectors.toList());
                this.getChildren().addAll(collect2);
            }
        }else{
            ServerDescriptor descriptor = (ServerDescriptor) templateDescriptor.descriptor.clone();
            replaceObject(descriptor, paramMap);
            this.serverDescriptor = descriptor;
            this.name=this.serverDescriptor.id;
            List<AdapterDescriptor> adapters = this.serverDescriptor.adapters;
            List<UIAdapter> collect = adapters.stream().map(
                    UIAdapter::new).collect(Collectors.toList());
            this.getChildren().addAll(collect);
        }
        param= (JSONObject) JSONObject.toJSON(serverDescriptor);
    }

    public UIServer(ServerDescriptor serverDescriptor,Map<String, TemplateDescriptor> serverTemplates,
                    Map<String,TemplateDescriptor> serviceTemplates) {
        this.type="SERVER";
        this.serverDescriptor = serverDescriptor;
        param= (JSONObject) JSONObject.toJSON(serverDescriptor);
        this.name = serverDescriptor.id;
        List<AdapterDescriptor> adapters = this.serverDescriptor.adapters;
        List<UIAdapter> collect = adapters.stream().map(
                UIAdapter::new).collect(Collectors.toList());
        this.getChildren().addAll(collect);
        if(IceBoxDescriptor.class.isAssignableFrom(serverDescriptor.getClass())){
            IceBoxDescriptor iceBoxDescriptor=(IceBoxDescriptor)serverDescriptor;
            this.serverTemplates=serverTemplates;
            this.serviceTemplates=serviceTemplates;
            List<ServiceInstanceDescriptor> services = iceBoxDescriptor.services;
            List<UIService> collect2 = services.stream().map(
                    item -> new UIService("", item,serverTemplates,serviceTemplates,null)).collect(Collectors.toList());
            this.getChildren().addAll(collect2);
        }
    }

}
